package net.airtheva.hexagonMatrix;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import net.airtheva.hexagonMatrix.data.Plane;
import net.airtheva.hexagonMatrix.data.Vector3f;
import net.airtheva.hexagonMatrix.data.Vector4f;
import net.airtheva.hexagonMatrix.data.Vertex;

public class Line extends BaseComponent {

	Vertex Point = null;
	Vector3f Vector = null;
	
	FloatBuffer mPositionBuffer = null;
	FloatBuffer mColorBuffer = null;
	
	Line(Surface surface, Vertex start, Vertex end) {
		super(surface);
		
		Point = (new Vertex())
			.Position(start.Position)
			.Color(1.0f, 0.0f, 0.0f, 1.0f);
		Vector = new Vector3f(new float[] {
			end.Position.Data[0] - start.Position.Data[0],
			end.Position.Data[1] - start.Position.Data[1],
			end.Position.Data[2] - start.Position.Data[2]
		});
		
		makeBuffer();
	}
	
	Line(Surface surface, Vertex point, Vector3f vector) {
		super(surface);
		
		Point = (new Vertex())
			.Position(point.Position)
			.Color(1.0f, 0.0f, 0.0f, 1.0f);
		Vector = new Vector3f(vector);
		
		makeBuffer();
	}
	
	protected void makeBuffer() {
		
		Vertex end = (new Vertex())
			.Position(
				Point.Position.Data[0] + Vector.Data[0],
				Point.Position.Data[1] + Vector.Data[1],
				Point.Position.Data[2] + Vector.Data[2]
			)
			.Color(0.0f, 0.0f, 1.0f, 1.0f);
		
		mPositionBuffer = ByteBuffer.allocateDirect(2 * Vector3f.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
		mColorBuffer = ByteBuffer.allocateDirect(2 * Vector4f.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
		
		mPositionBuffer.put(Point.Position.Data);
		mPositionBuffer.put(end.Position.Data);
		
		mColorBuffer.put(Point.Color.Data);
		mColorBuffer.put(end.Color.Data);
		
		mPositionBuffer.position(0);
		mColorBuffer.position(0);
		
	}
	
	@Override
	public void Draw(GL10 gl) {
		// TODO Auto-generated method stub
		
		preDraw(gl);
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mPositionBuffer);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
		
		gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 2);
		
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
	}
	
}
